<template>
  <div class="login-container">
    <div>
      <el-row>
        <el-col :span="12" :xs="0"></el-col>
        <el-col :span="12" :xs="24">
          <el-form ref="loginForms" class="login-form" :model="loginForm" :rules="rules">
            <h1>Hello</h1>
            <h2>欢迎来到门的世界</h2>
            <el-form-item prop="username">
              <el-input 
                :prefix-icon="User"
                v-model="loginForm.username"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input 
                type="password" 
                :prefix-icon="Lock"
                show-password
                v-model="loginForm.password"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button 
                :loading="loading"
                type="primary"
                class="login_button"
                @click="login"
              >登录</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {User, Lock} from '@element-plus/icons-vue'
import { reactive, ref } from 'vue';
import useUserStore from '@/store/modules/user'
import type { loginForm } from '@/api/user/type'
import { useRouter, useRoute } from 'vue-router';
import { ElNotification } from 'element-plus';
import { getLoginTime } from '@/utils/tiem'

let loginForm = reactive({
  username: 'admin',
  password: '111111',
})
let loading = ref(false)
let useStore = useUserStore()
let $router = useRouter()
let $route = useRoute()
const validatorName = (rule: any, value: any, callback: any) => {
  if( value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少为5位'))
  }
}
const validatorPass = (rule: any, value: any, callback: any) => {
  if( value.length <= 10) {
    callback()
  } else {
    callback(new Error('账号长度不超过10位'))
  }
}
const rules = {
  username: [
    { trigger: 'change', validator: validatorName }
  ],
  password: [
    { trigger: 'change', validator: validatorPass}
  ],
}
let loginForms = ref()

const login = async ()=>{  
  await loginForms.value.validate()
  loading.value = true
  try{
    await useStore.userLogin(loginForm)
    let redirect:any = $route.query.redirect
    $router.push({path: redirect || '/'})
    loading.value = false
    
    ElNotification({
      type: 'success',
      message: '欢迎来到门的世界',
      title: `Hi, ${getLoginTime()}好`
    })
  } catch(e) {
    loading.value = false
    $router.push('/login')
    ElNotification({
      type: 'error',
      message: (e as Error).message
    })
  }
}

</script>

<style lang="scss" scoped>
.login-container{
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login-form{
    position: relative;
    width: 80%;
    top: 30vh;
    padding: 40px;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;

    h1{
      color: white;
      font-size: 40px;
    }
    h2{
      color: white;
      font-size: 20px;
      margin: 20px 0;
    }
    .login_button{
      width: 100%;
    }
  }
}
</style>